//
//  AccentViewController.m
//  风景网
//
//  Created by mac on 15/10/27.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "AccentViewController.h"
#import "RequestXML.h"
#import "AccentModel.h"
#import "AccentCell.h"
#import "WebViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface AccentViewController (){

    NSMutableArray *_dataArrays;
    UITableView *_tableView;
    AccentModel *_accentModel;
    AVAudioPlayer *player;
}

@end

@implementation AccentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   

    [self loadData];
    [self createTableView];
}
#pragma mark-创建tableView
-(void)createTableView{
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    //注册
    UINib *nib = [UINib nibWithNibName:@"AccentCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:@"AccentCell"];
    
    [self.view addSubview:_tableView];
    
}

-(void)loadData{
    _dataArrays = [[NSMutableArray alloc]init];
    [RequestXML postxml:@"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><RequestServiceData xmlns=\"http://tempuri.org/\"><requestMessage>{\"RCode\":\"fengjing@#*!2014\",\"ClientType\":0,\"Module\":\"scenic\",\"Method\":\"GetScenicOfLocalVoice\",\"Data\":{\"PageIndex\":\"1\",\"PageSize\":\"20\",\"RecordCount\":0,\"Orderby\":{},\"QueryDict\":{\"key\":\"杭州\",\"voice\":\"1\",\"comments\":\"5\",\"Lat\":\"\",\"Lon\":\"\"},\"Query\":[]}}</requestMessage></RequestServiceData></soap:Body></soap:Envelope>" FinishBlock:^(NSDictionary *dic) {
        
        NSLog(@"%@",dic);
        NSArray *data = dic[@"Data"];
        for (NSDictionary *dicm in data) {
            AccentModel *accentModel = [[AccentModel alloc]initWithDataDic:dicm];
            [_dataArrays addObject:accentModel];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView reloadData];
        });
    }];
    
}
#pragma mark - UITabelView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_dataArrays.count != 0) {
        return _dataArrays.count;
    }
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //与导游界面共有的就不再重复创建，统一采用AccentCell
    AccentCell *cell = (AccentCell *)[tableView dequeueReusableCellWithIdentifier:@"AccentCell" forIndexPath:indexPath];
    if (_dataArrays.count != 0) {
        _accentModel = _dataArrays[indexPath.row];
        cell.accentModel = _accentModel;
        
        //创建下面的label_vioceCount.text = [NSString stringWithFormat:@"来自%@位网友的%@个评价",_accentModel.LocalVoiceCount,_accentModel.LocalVoiceTypeCount];
        UILabel *voiceCount = [[UILabel alloc]initWithFrame:CGRectMake(134, 45+54, 200,20)];
        voiceCount.text = [NSString stringWithFormat:@"来自%@位网友的%@个乡音",_accentModel.LocalVoiceCount,_accentModel.LocalVoiceTypeCount];
        voiceCount.font = [UIFont systemFontOfSize:13];
        voiceCount.textColor = [UIColor darkGrayColor];
        [cell.contentView addSubview:voiceCount];
        
    }
    
    //两个button是不一样的，所以在这里单独创建
    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [playButton setBackgroundImage:[UIImage imageNamed:@"imageBtnPlayForVoice"] forState:UIControlStateNormal];
    [playButton addTarget:self action:@selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
    playButton.frame = CGRectMake(134, 45, 59, 59);
    [cell.contentView addSubview:playButton];
    
    UIButton *voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [voiceButton setBackgroundImage:[UIImage imageNamed:@"icon_recording"] forState:UIControlStateNormal];
    [voiceButton addTarget:self action:@selector(voiceAction:) forControlEvents:UIControlEventTouchUpInside];
    voiceButton.frame = CGRectMake(134+59+50, 45, 59, 59);
    [cell.contentView addSubview:voiceButton];
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120;
}

#pragma mark- button方法
-(void)playAction:(UIButton *)btn{
    

    //构造网络url
    NSURL *url1=[NSURL URLWithString:@"http://m2.music.126.net/JTsYjGAojOUBZKRPURK_uQ==/7997847581905326.mp3"];

    player = [[AVAudioPlayer alloc]initWithContentsOfURL:url1 error:nil];
    
    [player play];
    
    
}
-(void)voiceAction:(UIButton *)btn{
    //录音
    
}
@end
